package repositories

import (
	"bbs-go/internal/models"
	"strconv"

	"github.com/mlogclub/simple/sqls"
	"github.com/mlogclub/simple/web/params"
	"gorm.io/gorm"
)

var UserLoginLogRepository = newUserLoginLogRepository()

func newUserLoginLogRepository() *userLoginLogRepository {
	return &userLoginLogRepository{}
}

type userLoginLogRepository struct {
}

func (r *userLoginLogRepository) Get(db *gorm.DB, id int64) *models.UserLoginLog {
	ret := &models.UserLoginLog{}
	if err := db.First(ret, "id = ?", id).Error; err != nil {
		return nil
	}
	return ret
}

func (r *userLoginLogRepository) Take(db *gorm.DB, where ...interface{}) *models.UserLoginLog {
	ret := &models.UserLoginLog{}
	if err := db.Take(ret, where...).Error; err != nil {
		return nil
	}
	return ret
}

func (r *userLoginLogRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserLoginLog) {
	cnd.Find(db, &list)
	return
}

func (r *userLoginLogRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.UserLoginLog {
	ret := &models.UserLoginLog{}
	if err := cnd.FindOne(db, &ret); err != nil {
		return nil
	}
	return ret
}

func (r *userLoginLogRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.UserLoginLog, paging *sqls.Paging) {
	return r.FindPageByCnd(db, &params.Cnd)
}

func (r *userLoginLogRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserLoginLog, paging *sqls.Paging) {
	cnd.Find(db, &list)
	count := cnd.Count(db, &models.UserLoginLog{})

	paging = &sqls.Paging{
		Page:  cnd.Paging.Page,
		Limit: cnd.Paging.Limit,
		Total: count,
	}
	return
}

func (r *userLoginLogRepository) GetUserLoginLogList(db *gorm.DB, cnd *sqls.Cnd, nickname string) (list []models.UserLoginLogResponse, paging *sqls.Paging, err error) {
	limit := cnd.Paging.Limit
	offset := cnd.Paging.Limit * (cnd.Paging.Page - 1)
	if limit == 0 {
		limit = 10
	}

	querySql := `select tull.id,tull.create_time as login_time,tu.nickname,tud.name as department_name 
	from t_user_login_log tull 
	left join t_user tu on tull.user_id = tu.id 
	left join t_user_department tud on tu.department_id = tud.id 
	where 1=1 `
	whereSql := ""
	whereVars := []interface{}{}
	if nickname != "" {
		whereSql = " and tu.nickname like ? "
		whereVars = append(whereVars, "%"+nickname+"%")
	}

	countSql := `select count(1)
	from t_user_login_log tull 
	left join t_user tu on tull.user_id = tu.id 
	where 1=1 `

	var total int64
	err = db.Raw(countSql+whereSql, whereVars...).Scan(&total).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, nil, err
	}

	dblist := []models.UserLoginLogResponse{}
	orderBySql := " order by tull.id desc LIMIT " + strconv.Itoa(limit) + " OFFSET " + strconv.Itoa(offset)
	err = db.Raw(querySql+whereSql+orderBySql, whereVars...).Find(&dblist).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, nil, err
	}

	return dblist, &sqls.Paging{
		Page:  cnd.Paging.Page,
		Limit: limit,
		Total: total,
	}, nil
}

func (r *userLoginLogRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.UserLoginLog) {
	db.Raw(sqlStr, paramArr...).Scan(&list)
	return
}

func (r *userLoginLogRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
	db.Raw(sqlStr, paramArr...).Count(&count)
	return
}

func (r *userLoginLogRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
	return cnd.Count(db, &models.UserLoginLog{})
}

func (r *userLoginLogRepository) Create(db *gorm.DB, t *models.UserLoginLog) (err error) {
	err = db.Create(t).Error
	return
}

func (r *userLoginLogRepository) Update(db *gorm.DB, t *models.UserLoginLog) (err error) {
	err = db.Save(t).Error
	return
}

func (r *userLoginLogRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
	err = db.Model(&models.UserLoginLog{}).Where("id = ?", id).Updates(columns).Error
	return
}

func (r *userLoginLogRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
	err = db.Model(&models.UserLoginLog{}).Where("id = ?", id).UpdateColumn(name, value).Error
	return
}

func (r *userLoginLogRepository) Delete(db *gorm.DB, id int64) {
	db.Delete(&models.UserLoginLog{}, "id = ?", id)
}
